/////////////////////////////////////////////////////////////////////////////
// Name:        ax_module_book.h
// Purpose:     [E]Book module classes
// Author:      Julian Smart
// Modified by:
// Created:     2009-03-20
// RCS-ID:
// Copyright:   (c) Julian Smart
// Licence:     New BSD License
/////////////////////////////////////////////////////////////////////////////

#ifndef _AX_MODULE_BOOK_H_
#define _AX_MODULE_BOOK_H_

/*
 * Includes
 */

#include "ax_resource/ax_resource.h"
#include "ax_resource/ax_activator_resource.h"
#include "ax_resource/ax_module.h"

class ebBasicInfo;

class wxTextOutputStream;

/**
    @class AxBookResource

    A resource for ebook files.

    @category{ax_resource}
 */

class AxBookResource: public AxResource
{
    DECLARE_DYNAMIC_CLASS( AxBookResource )

public:
// Constructors

    AxBookResource(const AxBookResource& resource) { Init(); Copy(resource); }
    AxBookResource() { Init(); }
    ~AxBookResource();

// Operations

    void Init();

    /// Copy the resource information.
    void Copy(const AxBookResource& resource);

    /// Assign from resource
    void operator=(const AxBookResource& resource) { Copy(resource); }

    /// Copy from epub information.
    void Copy(const ebBasicInfo& info);

    /// Assign from epub info
    void operator=(const ebBasicInfo& info) { Copy(info); }

    /// Clone - derived classes must implement this function.
    virtual AxDocument* Clone() const { return new AxBookResource(*this); }

    /// Is this an epub?
    bool IsEpub() const;

protected:

};

/**
    @class AxBookView

    Shows ebook view.

    @category{ax_resource}
 */

class AxSummaryViewPanel;
class AxBookView: public AxView
{
    DECLARE_DYNAMIC_CLASS( AxBookView )

public:
// Constructors

    AxBookView() { Init(); }
    ~AxBookView() {}

// Operations

    void Init();

    virtual bool Activate(AxViewManager* viewManager, AxDocument* doc);
    virtual bool Deactivate(AxViewManager* viewManager);

    virtual bool Initialize(AxViewManager* viewManager);
    virtual bool Uninitialize(AxViewManager* viewManager);
    
    void ShowViewIfInactive();

    AxBookResource* GetResource() const;

    AxSummaryViewPanel* GetViewPanel() const;

    void SetDescription(const wxString& text);

    bool ShowBookDetails(AxBookResource* resource, bool say = true);
    bool ShowBookSection(AxBookResource* resource, const wxString& sectionLocation, bool say = true);

    // Remove HTML tags from file, outputting text to stream
    bool ConvertHTMLToText(const wxString& filename, wxOutputStream& stream, int options = 0);

protected:

};

/**
    @class AxBookModule

    A module for dealing with book files.

    @category{ax_resource}
 */

class AxBookModule: public AxModule
{
DECLARE_DYNAMIC_CLASS(AxBookModule)

public:
    /// Constructor.
    AxBookModule();

    /// Destructor.
    ~AxBookModule() {}

// Overridables

    /// Called on IDE startup, before the main window has been created.
    virtual bool OnInit();

    /// Called on application startup, after the main window has been created.
    virtual bool OnInitUI();

 	/// Called just before program termination, but only if OnInit succeeded.
    virtual void OnExit();

	/// Read config settings
	virtual bool LoadConfig(wxConfigBase& config);

	/// Write config settings
    virtual bool SaveConfig(wxConfigBase& config);

    /// Called just before the frame is closed. Returning false vetoes
    /// the frame/app close.
    virtual bool OnCloseMainWindow() { return true; }

    /// Allows app to search amongst its own objects. Call AxResourceLibrary::AddSearchItem
    /// for each item.
    virtual bool OnSearch(const wxString& WXUNUSED(searchString), int WXUNUSED(flags), AxResourceLibrary* WXUNUSED(library)) { return false; }

    /// Is this file supported?
    virtual bool IsFileSupported(const wxString& filename);

    /// Load the file into a new AxResource
    virtual AxResource* LoadResource(const wxString& filename);

    /// Aggregate any files that belong together and add to resources. Return false if you don't
    /// aggregate any files.
    virtual bool AggregateFiles(AxResourceLibrary* library, wxArrayString& files, bool checkIfFileLoaded);

    /// Create an activator for the resource, for creating views and further menu items.
    virtual AxActivator* CreateActivator(AxResourceLibrary* library, AxResource* resource);

    /// Load information from epub file
    static bool LoadEpubBookInfo(const wxString& filename, AxBookResource& resource, bool makeThumbnail, const wxSize& thumbnailImageSize, bool& foundCoverImage);

// Accessors

protected:
};

/**
    @class AxBookResourceActivator

    Activates an individual book resource.

    @category{ax_resource}
 */

class AxBookResourceActivator: public AxResourceActivator
{
public:
    
    DECLARE_DYNAMIC_CLASS( AxBookResourceActivator )

public:
// Constructors

    AxBookResourceActivator() { Init(); }
    AxBookResourceActivator(const AxBookResourceActivator& activator) { Init(); Copy(activator); }

    AxBookResourceActivator(AxResourceLibrary* library, AxResource* resource);
    ~AxBookResourceActivator() {}

// Operations

    void Init();

    void Copy(const AxBookResourceActivator& activator);

    virtual AxActivator* Clone() const { return new AxBookResourceActivator(*this); }

    virtual AxMenu* Activate(AxMenuController* controller, AxMenuItem* item);

    /// Called when a menu is reactivated, for example by going up. This
    /// allows a view to be shown again, without generating all the menus.
    virtual bool Reactivate(AxMenuController* controller, AxMenuItem* item);

    AxBookResource* GetBookResource() const;

protected:
};

/**
    @class AxBookResourceCommandActivator

    This activator deals with book commands and sections.

    @category{ax_resource}
 */

typedef enum 
{
    AxBookResourceCommandUnknown,

    AxBookResourceCommandShowSection,
    AxBookResourceCommandPlay,
    AxBookResourceCommandStop,
    AxBookResourceCommandShowDetails,

} AxBookResourceCommand;

class AxBookResourceCommandActivator: public AxResourceActivator
{
public:
    
    DECLARE_DYNAMIC_CLASS( AxBookResourceCommandActivator )

public:
// Constructors

    AxBookResourceCommandActivator() { Init(); }
    AxBookResourceCommandActivator(const AxBookResourceCommandActivator& activator) { Init(); Copy(activator); }

    AxBookResourceCommandActivator(AxBookResourceCommand cmd, AxResourceLibrary* library, AxResource* resource,
        const wxString& sectionLocation = wxEmptyString);
    ~AxBookResourceCommandActivator() {}

// Operations

    void Init();

    void Copy(const AxBookResourceCommandActivator& activator);

    virtual AxActivator* Clone() const { return new AxBookResourceCommandActivator(*this); }

    virtual AxMenu* Activate(AxMenuController* controller, AxMenuItem* item);

    AxBookResource* GetBookResource() const;

protected:
    AxBookResourceCommand   m_command;
    wxString                m_sectionLocation;

};

// Remove redundant symbols from ISBN number
wxString AxMakeISBNGood(const wxString& isbn);

// Is this a valid ISBN?
bool AxIsISBN(const wxString& isbn);

#endif
    // _AX_MODULE_BOOK_H_
